from gideon.service.starlabs import GideonHandler


class ClassListHandler(GideonHandler):
    display_list = ["school", "course", "price", "start_date", "class_teacher", "tech_teachers", "course_record"]

    def saving_post_handler(self, form, is_update=False, *args, **kwargs):
        if not is_update:
            form.save_m2m()
        return super().saving_post_handler(form, is_update=False, *args, **kwargs)

    def show_course_field(self, obj=None, is_title=False, *args, **kwargs):
        if is_title:
            return "班级"
        else:
            return f"{obj.course}({obj.semester}期)", "body", None

    def show_start_date_field(self, obj=None, is_title=False, *args, **kwargs):
        return self.format_datetime_field(obj, "start_date", is_title=is_title, *args, **kwargs)

    def show_course_record_field(self, obj=None, is_title=False, *args, **kwargs):
        title = "上课记录"
        if is_title:
            return title
        if obj:
            url = self.reverse_handle_url(self.request, "supergirl_courserecord_list", class_key=obj.key)
            return {"href": url, "title": title}, "body", {"type": "link", "target": "_blank"}
        return "scord function error"

    def show_tech_teachers_field(self, obj=None, is_title=False, *args, **kwargs):
        return self.format_m2m_field(obj, "tech_teachers", is_title, *args, **kwargs)
